package org.bigtree.example.common.frame.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * 日期格式序列化和反序列化
 *
 * @author bigTree
 * @since 2025/5/3
 */
@Configuration
public class DateFormatConfig {
    @Value("${spring.jackson.date-format}")
    private String dateFormat;
    @Value("${spring.jackson.time-zone}")
    private String timeZone;

    @Bean
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        return builder
                .serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateFormat)))
                .deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateFormat)))
                .timeZone(TimeZone.getTimeZone(timeZone))
                .build();
    }
}
